package com.sanrenzu.chunbo.dao;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;
import com.sanrenzu.chunbo.utils.ORMliteHelper;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangke on 16/7/16.
 * private Dao<User,Integer>
 * User,代表实体类
 * Integer 主键的类型
 * <p/>
 * 1获取的dao
 * 2增删改查
 */
public class ShopDao {
    private ORMliteHelper orMliteHelper;
    private static Dao<Collection, Integer> dao;
    private static final String TAG = "TAG";
    private static ShopDao uDao;

    private ShopDao(Context context) {
        this.orMliteHelper = new ORMliteHelper(context);
        try {
            dao = orMliteHelper.getDao(Collection.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static ShopDao userDaoInstence(Context context) {
        if (uDao == null) {
            uDao = new ShopDao(context);
        }
        return uDao;
    }


    public static void add(Collection collection, Context context) {
        userDaoInstence(context);
        try {
            int state = dao.create(collection);

            Log.i(TAG, "add: " + state);//

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void del(Context context,String productId) {
        userDaoInstence(context);
        try {
            DeleteBuilder builder = dao.deleteBuilder();
            builder.where().eq("productId",productId);

            int state = builder.delete();
            Log.i(TAG, "del: " + state);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void update(Context context,Collection collection) {
        userDaoInstence(context);
        try {
            UpdateBuilder builder = dao.updateBuilder();
            builder.where().eq("productId", collection.getProductId());
//            builder.updateColumnValue("password", "oooo");
            int state = builder.update();
            Log.i(TAG, "update: " + state);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static List<Collection> quaryAll(Context context) {
        List<Collection> listuUsers=new ArrayList<>();
        userDaoInstence(context);
        try {
             listuUsers= dao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return listuUsers;
    }
    public static List<Collection> quaryOne(Context context,String productId) {
        List<Collection> listuUsers=new ArrayList<>();
        userDaoInstence(context);
        try {

            QueryBuilder builder=dao.queryBuilder();

            builder.where().eq("productId",productId);
             listuUsers=builder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return listuUsers;
    }



}
